﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LeakView.service;
using LeakView.view.controls;
using LeakView.common;

namespace LeakView.misc.view.controls
{
    public partial class HistorySSSC : UserControl
    {

        private MiscType _theMiscType = MiscType.DeltaNOW;

        [Browsable(true)]
        [DescriptionAttribute("设置或获取MISC类型")]
        public MiscType TheMiscType
        {
            get
            {
                return _theMiscType;
            }
            set
            {
                this._theMiscType = value;
                this.historyChartSingle1.Title = GetMiscTypeString(value);
            }
        }

        public HistorySSSC()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {

            if (!this.historyTimeRanger1.IsValid)
            {
                MessageBox.Show("参数错误，起始时间必须小于结束时间！", "错误");
                return;
            }

            try
            {
                using (new LoadingStarter())
                {
                    LoadData();
                }
            }
            catch
            {
                MessageBox.Show("获取数据失败，请确定服务器是否正常！", "错误");
            }

        }

        private string GetMiscTypeString(MiscType type)
        {
            switch (type)
            {
                case MiscType.DeltaNOW: return "瞬时输差";
                case MiscType.Delta10M: return "10分钟输差";
                case MiscType.Delta30M: return "30分钟输差";
                case MiscType.Delta60M: return "60分钟输差";
                default: return type.ToString();
            }
        }

        private void LoadData()
        {
            this.historyChartSingle1.clear();

            var date = this.historyTimeRanger1.Date;
            var data = MiscModal.me.LoadHistory(TheMiscType, date.Year, date.Month, date.Day);

            var start = historyTimeRanger1.StartTime;
            var end = historyTimeRanger1.EndTime;

            this.historyChartSingle1.Title = string.Format("{0} ({1} - {2})", GetMiscTypeString(TheMiscType), start.ToString("yyyy-MM-dd HH:mm"), end.ToString("yyyy-MM-dd HH:mm"));
            this.historyChartSingle1.SetTimeRange(start, end);

            data.ForEach(d =>
            {
               this.historyChartSingle1.appendValue(Tools.ConvertLongToDateTime(d.timestamp),
                                MiscModal.me.GetDoubleFromMiscValue(d));

            });
            
        }
    }
}
